package org.ws.task.schedule.dispatcher;

import com.google.common.collect.Lists;
import org.apache.commons.lang.math.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.ws.task.model.NodeMetadata;
import org.ws.task.model.TaskConfig;
import org.ws.task.model.TaskItem;

import java.util.List;

public class RandomDispatcher extends AbstractDispatcher{

    private static Logger log = LoggerFactory.getLogger(RandomDispatcher.class.getName());

    @Override
    public TaskConfig.DispatchStrategy strategy() {
        return TaskConfig.DispatchStrategy.RANDOM;
    }

    @Override
    protected List<NodeMetadata> getNode(TaskItem item,List<NodeMetadata> taskWorkerNodes) {
        final int size = taskWorkerNodes.size();
        int random = RandomUtils.nextInt(9999);
        int index = random % size;
        log.debug("size:{},random:{},index:{}",size,random,index);
        return Lists.newArrayList(taskWorkerNodes.get(index));
    }
}
